package co.yixiang.modules.aliyun;

import co.yixiang.api.ApiResult;
import co.yixiang.api.YshopException;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@Api(value = "上传文件", tags = "公共:上传文件", description = "上传文件")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AliUploadController {

    @PostMapping("/uploadService")
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public ApiResult uploadService(@RequestPart("file") MultipartFile picture) {
        JSONObject resObj = new JSONObject();
        Map map = new HashMap();
        String pictureURL = "";
        try {
            OSSUtil ossUtil = new OSSUtil();
            pictureURL = ossUtil.upload(picture);
        } catch (Exception e) {
            resObj.put("msg", "error");
            throw new YshopException("上传失败");
        }
        resObj.put("msg", "http://jiayixuan.oss-cn-beijing.aliyuncs.com/"+pictureURL);

        map.put("code", 0);
        map.put("data","http://jiayixuan.oss-cn-beijing.aliyuncs.com/"+pictureURL);
        return ApiResult.ok(map);
    }
}
